package com.aeromedia.testcustomapplication;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.WindowDecorActionBar;
import androidx.appcompat.widget.Toolbar;
import androidx.appcompat.widget.ToolbarWidgetWrapper;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import java.lang.reflect.Field;

public class MainActivity extends AppCompatActivity {
    private static final int PERMISSION_REQ_ID = 123;
    private static final String[] REQUESTED_PERMISSIONS = {Manifest.permission.RECORD_AUDIO,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.ACCESS_COARSE_LOCATION,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.WRITE_CONTACTS,
            Manifest.permission.READ_CALL_LOG,
            Manifest.permission.WRITE_CALL_LOG,
            Manifest.permission.ACCESS_FINE_LOCATION};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actionBarChange();
        checkPermissions();

    }

    @SuppressLint({"ResourceAsColor", "RestrictedApi"})
    private void actionBarChange() {
        if (getSupportActionBar() != null) {
//            getSupportActionBar().setDisplayShowHomeEnabled(true);
            Class<? extends ActionBar> windowDecorActionBarClazz = (getSupportActionBar()).getClass();
            Field mDecorToolbar = null;
            try {
                mDecorToolbar = windowDecorActionBarClazz.getDeclaredField("mDecorToolbar");
                mDecorToolbar.setAccessible(true);
                ToolbarWidgetWrapper toolbarWidgetWrapper = (ToolbarWidgetWrapper) mDecorToolbar.get(((WindowDecorActionBar) getSupportActionBar()));
//                toolbarWidgetWrapper.setDefaultNavigationIcon(getResources().getDrawable(R.drawable.idle_state));
                toolbarWidgetWrapper.setNavigationIcon(getResources().getDrawable(R.drawable.custom_ic_ab_back_material));
                @SuppressLint("RestrictedApi") Toolbar toolbar = (Toolbar) toolbarWidgetWrapper.getViewGroup();
                toolbar.setTitleTextColor(R.color.black);
//            toolbar.setLogo(getResources().getDrawable(R.drawable.idle_state));


            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            StatusBarUtils.setActivityTranslucent(this);
        }
    }

    private static final String TAG = "MainActivity";

    private void checkPermissions() {
        Log.d(TAG, "checkPermissions: ");

        if (checkSelfPermission(REQUESTED_PERMISSIONS[0], PERMISSION_REQ_ID) &&
                checkSelfPermission(REQUESTED_PERMISSIONS[1], PERMISSION_REQ_ID) &&
                checkSelfPermission(REQUESTED_PERMISSIONS[2], PERMISSION_REQ_ID) &&
                checkSelfPermission(REQUESTED_PERMISSIONS[3], PERMISSION_REQ_ID) &&
                checkSelfPermission(REQUESTED_PERMISSIONS[4], PERMISSION_REQ_ID) &&
                checkSelfPermission(REQUESTED_PERMISSIONS[5], PERMISSION_REQ_ID) &&
                checkSelfPermission(REQUESTED_PERMISSIONS[6], PERMISSION_REQ_ID) &&
                checkSelfPermission(REQUESTED_PERMISSIONS[7], PERMISSION_REQ_ID) &&
                checkSelfPermission(REQUESTED_PERMISSIONS[8], PERMISSION_REQ_ID) &&
                checkSelfPermission(REQUESTED_PERMISSIONS[9], PERMISSION_REQ_ID)
        ) {
            Log.d(TAG, "checkPermissions: startActivity");
            startActivity(new Intent(this, CustomServiceActivity.class));
        } else {
            ActivityCompat.requestPermissions(this,
                    REQUESTED_PERMISSIONS,
                    PERMISSION_REQ_ID);
        }
    }

    public boolean checkSelfPermission(String permission, int requestCode) {
        if (ContextCompat.checkSelfPermission(this,
                permission)
                != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(this,
                    REQUESTED_PERMISSIONS,
                    requestCode);
            return false;
        }
        return true;
    }

    public void jumpToServiceActivity(View view) {
        startActivity(new Intent(this, CustomServiceActivity.class));
    }
}